<?php
namespace App\Model;
use App\Model\User as Model_User;

use PhalApi\Model\NotORMModel as NotORM;

class Zlive extends NotORM {
	
	/* 直播间列表 */
    public function getLivelist($p, $country='',$language='',$uid='') {
        if($p<1){
            $p=1;
        }
        $pnum=50;
        $start=($p-1)*$pnum;
		
		$where=" islive= '1'";
		/*if($country){
		    $where .= " and country= '".$country."'";
		}
		
		if($language){
		    $where .= " and language= '".$language."'";
		}*/
		$order1 = '';
		$order2 = '';
		if($country){
		    $order1 .= "field(country,'".$country."') desc";
		}
		if($language){
		    $order2 .= "field(language,'".$language."') desc";
		}
		$result=\PhalApi\DI()->notorm->live
                    ->select('uid,title,city,stream,pull,thumb,isvideo,type,type_val,anyway,starttime,country,language')
                    ->where($where)
                    ->order($order1)
                    ->order($order2)
                    ->order('recommend_val desc,starttime desc')
                    ->limit($start,$pnum)
                    ->fetchAll();            
		foreach($result as $k=>$v){
		    $v['userInfo'] = \App\getUserInfo($v['uid']);//guo
		    $v['isAttention'] =  \App\isAttention($uid,$v['uid']);
			$v = \App\handleLive($v);
            $result[$k]=$v;
		}	
		
		return $result;
    }
    
    public function getAttentionLivelist($p, $uid) {
        if($p<1){
            $p=1;
        }
        $pnum=50;
        $start=($p-1)*$pnum;

		$attention_list = \PhalApi\DI()->notorm->user_attention
		            ->select('touid')
		            ->where('uid='.$uid)
		            ->fetchAll();
		
		$attention = [];
		foreach ($attention_list as $value){
		    $attention[] = $value['touid'];
		}


		if($attention){
		    $where=" islive= 1 and uid IN (".implode(',',$attention).")";
//		    $whereAttention = ['uid', 'in', $attention];
		    $result=\PhalApi\DI()->notorm->live
                    ->select('uid,title,city,stream,pull,thumb,isvideo,type,type_val,anyway,starttime')
                    ->where($where)
//                    ->where($whereAttention)
                    ->order('recommend_val desc,starttime desc')
                    ->limit($start,$pnum)
                    ->fetchAll();
            foreach($result as $k=>$v){
                $v=\App\handleLive($v);
                $result[$k]=$v;
            }

            return $result;
		}else{
		    return [];
		}
		
		
    }
	
	
	 /* 分类下直播 */
    public function getClassLive($liveclassid,$p) {
        if($p<1){
            $p=1;
        }
		$pnum=50;

		$start=0;
		$where=" islive='1' and liveclassid={$liveclassid}";
        
		$result=\PhalApi\DI()->notorm->live
				->select('uid,title,city,stream,pull,thumb,isvideo,type,type_val,anyway,starttime')
				->where($where)
				->order("starttime desc")
				->limit(0,$pnum)
				->fetchAll();	
		foreach($result as $k=>$v){
			$v=\App\handleLive($v);     
            $result[$k]=$v;
		}			

		return $result;
    }
	
	/* 创建房间 */
	public function createRoom($uid,$data) {
        

		$isexist=\PhalApi\DI()->notorm->live
					->select("uid,isvideo,islive,stream")
					->where('uid=?',$uid)
					->fetchOne();
		if($isexist){
            /* 判断存在的记录是否为直播状态 */
            if($isexist['isvideo']==0 && $isexist['islive']==1){
                /* 若存在未关闭的直播 关闭直播 */
                $this->stopRoom($uid,$isexist['stream']);
                /* 加入 */
                $rs=\PhalApi\DI()->notorm->live->insert($data);
            }else{
                /* 更新 */
                $rs=\PhalApi\DI()->notorm->live->where('uid = ?', $uid)->update($data);
                if ($data['islive'] == 1) {
                    $configpri=\App\getConfigPri();
                    $dailytask_switch=$configpri['dailytask_switch'];
                    if($dailytask_switch){
                        $key='open_live_daily_tasks_'.$uid;
                        $room_time=time();
                        \App\setcaches($key,$room_time);
                    }
                }

            }
		}else{
			/* 加入 */
			$rs=\PhalApi\DI()->notorm->live->insert($data);
		}
		return $rs;

	}
	
	
	/* 修改直播状态 */
	public function changeLive($uid,$stream,$status){

		if($status==1){
            $info=\PhalApi\DI()->notorm->live
                    ->select("*")
					->where('uid=? and stream=?',$uid,$stream)
                    ->fetchOne();

            if($info){
                \PhalApi\DI()->notorm->live
					->where('uid=? and stream=?',$uid,$stream)
					->update(array("islive"=>1));

				//每日任务--开播
				$configpri=\App\getConfigPri();
				$dailytask_switch=$configpri['dailytask_switch'];

				if($dailytask_switch){
					$key='open_live_daily_tasks_'.$uid;
					$room_time=time();
					\App\setcaches($key,$room_time);
				}


            }
			return $info;
		}else{
			$this->stopRoom($uid,$stream);
			return 1;
		}
	}
	
	
	/* 关播 */
	public function stopRoom($uid,$stream) {

		$info=\PhalApi\DI()->notorm->live
				->select("uid,showid,starttime,title,province,city,stream,lng,lat,type,type_val,liveclassid,deviceinfo")
				->where('uid=? and stream=? and islive="1"',$uid,$stream)
				->fetchOne();
		if($info){
			$isdel=\PhalApi\DI()->notorm->live
				->where('uid=?',$uid)
				->delete();
            if(!$isdel){
                return 0;
            }
			$nowtime=time();
			$info['endtime']=$nowtime;
			$info['time']=date("Y-m-d",$info['showid']);
			$votes=\PhalApi\DI()->notorm->user_voterecord
				->where('uid =? and showid=?',$uid,$info['showid'])
				->sum('total');
			$info['votes']=0;
			if($votes){
				$info['votes']=$votes;
			}
				
			\App\delcache("livelist",$uid);
			\App\delcache('attention_'.$uid);
			
			
			$nums=\App\zCard('user_'.$stream);
			$info['nums']=$nums;			
			$result=\PhalApi\DI()->notorm->live_record->insert($info);	
			
			\App\delcache('user_'.$stream);

            /* 解除本场禁言 */
            $list2=\PhalApi\DI()->notorm->live_shut
                ->select('uid')
                ->where('liveuid=? and showid!=0',$uid)
                ->fetchAll();
            \PhalApi\DI()->notorm->live_shut->where('liveuid=? and showid!=0',$uid)->delete();
            
            foreach($list2 as $k=>$v){
                \App\delcache($uid . 'shutup',$v['uid']);
            }

            //每日任务--直播
            $key='open_live_daily_tasks_'.$uid;
          	$starttime=\App\getcaches($key);

          	if($starttime){
          		$endtime=time();

          		$daily_data=[
					'type'=>'open_live',
					'starttime'=>$starttime,
					'endtime'=>$endtime,
				];
				\App\dailyTasks($uid,$daily_data);
				\App\delcache($key);
          	}
            
		}
		return 1;
	}
	
	/* 关播信息 */
	public function stopInfo($stream){
		
		$rs=array(
			'nums'=>0,
			'length'=>0,
			'votes'=>0,
		);
		
		$stream2=explode('_',$stream);
		$liveuid=$stream2[0];
		$starttime=$stream2[1];
		$liveinfo=\PhalApi\DI()->notorm->live_record
					->select("starttime,endtime,nums,votes")
					->where('uid=? and stream=?',$liveuid,$stream)
					->fetchOne();
		if($liveinfo){
            $cha=$liveinfo['endtime'] - $liveinfo['starttime'];
			$rs['length']=\App\getSeconds($cha,1);
			$rs['nums']=$liveinfo['nums'];
		}
		if($liveinfo['votes']){
			$rs['votes']=$liveinfo['votes'];
		}
		return $rs;
	}
	
	/* 直播状态 */
	public function checkLive($uid,$liveuid,$stream){
        
        /* 是否被踢出 */
        $isexist=\PhalApi\DI()->notorm->live_kick
					->select("id")
					->where('uid=? and liveuid=?',$uid,$liveuid)
					->fetchOne();
        if($isexist){
            return 1008;
        }
		$islive=\PhalApi\DI()->notorm->live
					->select("islive,type,type_val,starttime")
					->where('uid=? and stream=?',$liveuid,$stream)
					->fetchOne();

		if(!$islive || $islive['islive']==0){
			return 1005;
		}


		$rs['type']=$islive['type'];
		$rs['type_val']='0';
		$rs['type_msg']='';

		$model_user=new Model_User();
		$checkTeenager = $model_user->checkTeenager($uid);
		$teenager_status=$checkTeenager['info'][0]['status'];


		if($uid>0){
			$userinfo=\PhalApi\DI()->notorm->user
				->select("issuper")
				->where('id=?',$uid)
				->fetchOne();
			if($userinfo && $userinfo['issuper']==1 && !$teenager_status){  //超管身份、非青少年模式下
				$rs['type']='0';
				$rs['type_val']='0';
				$rs['type_msg']='';
				
				return $rs;
			}
		}
		

		$configpub=\App\getConfigPub();
		
		if($islive['type']==1){
			$rs['type_msg']=md5($islive['type_val']);
			
		}else if($islive['type']==2){

			$rs['type_msg']=\PhalApi\T('本房间为收费房间，需支付').$islive['type_val'].$configpub['name_coin'];
			$rs['type_val']=$islive['type_val'];

			
			//打开青少年模式
			if($teenager_status==1){
				return $rs;
			}

			$isexist=\PhalApi\DI()->notorm->user_coinrecord
						->select('id')
						->where('uid=? and touid=? and showid=? and action=9 and type=0',$uid,$liveuid,$islive['starttime'])
						->fetchOne();
			if($isexist){
				$rs['type']='0';
				$rs['type_val']='0';
				$rs['type_msg']='';
			}
		}else if($islive['type']==3){

			$rs['type_val']=$islive['type_val'];
			$rs['type_msg']=\PhalApi\T('本房间为计时房间，每分钟需支付').$islive['type_val'].$configpub['name_coin'];
		}
		
		return $rs;
		
	}
	
	
	/* 用户余额 */
	public function getUserCoin($uid){
		$userinfo=\PhalApi\DI()->notorm->user
					->select("coin")
					->where('id=?',$uid)
					->fetchOne();
		return $userinfo;
	}
	
	/* 房间扣费 */
	public function roomCharge($uid,$liveuid,$stream){
		$islive=\PhalApi\DI()->notorm->live
					->select("islive,type,type_val,starttime")
					->where('uid=? and stream=?',$liveuid,$stream)
					->fetchOne();
		if(!$islive || $islive['islive']==0){
			return 1005;
		}
		if($islive['type']==0 || $islive['type']==1 ){
			return 1006;
		}		
		$total=$islive['type_val'];
		if($total<=0){
			return 1007;
		}
        
        /* 更新用户余额 消费 */
		$ifok=\App\upCoin($uid,$total);
        if(!$ifok){
            return 1008;
        }

		$action='9';
		$v_action='12';
		if($islive['type']==3){
			$action='10';
			$v_action='13';
		}
		
		$giftid=0;
		$giftcount=0;
		$showid=$islive['starttime'];
		$addtime=time();
		

		/* 更新直播 映票 累计映票 */
		\App\addVotes($liveuid,$total,$total);
        $insert_votes=[
            'type'=>'1',
            'action'=>$v_action,
            'uid'=>$liveuid,
            'fromid'=>$uid,
            'actionid'=>$giftid,
            'nums'=>$giftcount,
            'total'=>$total,
            'showid'=>$showid,
            'votes'=>$total,
            'addtime'=>time(),
        ];
		\App\addVoteRecord($insert_votes);


		/* 更新直播 映票 累计映票 */
		$insert_coin=[
			"type"=>'0',
			"action"=>$action,
			"uid"=>$uid,
			"touid"=>$liveuid,
			"actionid"=>$giftid,
			"nums"=>$giftcount,
			"totalcoin"=>$total,
			"showid"=>$showid,
			"addtime"=>$addtime
		];

		\App\addCoinRecord($insert_coin);
		
				
		$userinfo2=\PhalApi\DI()->notorm->user
					->select('coin,consumption')
					->where('id = ?', $uid)
					->fetchOne();
	
		$level=\App\getLevel($userinfo2['consumption']);

		$rs['coin']=$userinfo2['coin'];
		$rs['level']=$level;
		return $rs;
		
	}

	/* 赠送礼物 */
	public function sendGift($uid,$liveuid,$stream,$giftid,$giftcount) {

        /* 礼物信息 */
		$giftinfo=\PhalApi\DI()->notorm->gift
					->select("*")
					->where('id=?',$giftid)
					->fetchOne();
		if(!$giftinfo){
			/* 礼物信息不存在 */
			return 1002;
		}
		
		//语言包
		/*$lang=\PhalApi\DI()->lang;
		if($lang=='en'){
			$giftinfo['name']=$giftinfo['name_en'];
		}*/

		$stream2=explode('_',$stream);
        $showid=$stream2[1];

        $total=$giftinfo['needcoin']*$giftcount;
		 
		$addtime=time();
		$type='0';
		$action='1';
        
	    /* 更新用户余额 消费 */
	    $ifok=\App\upCoin($uid,$total);
		if(!$ifok){
			/* 余额不足 */
			return 1001;
		}
		
		$insert_coin=[
			"type"=>$type,
			"action"=>$action,
			"uid"=>$uid,
			"touid"=>$liveuid,
			"actionid"=>$giftid,
			"nums"=>$giftcount,
			"totalcoin"=>$total,
			"showid"=>$showid,
			"addtime"=>$addtime
		];
		\App\addCoinRecord($insert_coin);

        $gifttoken=md5(md5($action.$uid.$liveuid.$giftid.$giftcount.$total.$showid.$addtime.rand(100,999)));

        $swf=$giftinfo['swf'] ? \App\get_upload_path($giftinfo['swf']):'';

        $result=[];
        $result['gifttoken']=$gifttoken;
		

		// 家族分成之后的金额
		$anthor_total=$total;
		if($anthor_total){
			$anthor_total=\App\setFamilyDivide($liveuid,$anthor_total,$showid,'sendgift');
		}
		
		// 更新用户 魅力值 累计魅力值
		\App\addVotes($liveuid,$anthor_total,$total);
		$insert_votes=[
            'type'=>'1',
            'action'=>'3',
            'uid'=>$liveuid,
            'fromid'=>$uid,
            'actionid'=>$giftid,
            'nums'=>$giftcount,
            'total'=>$total,
            'showid'=>$showid,
            'votes'=>$anthor_total,
            'addtime'=>time(),
        ];
		\App\addVoteRecord($insert_votes);
		
		
		$ispk='0';
		$pkuid1='0';
		$pkuid2='0';
		$pktotal1='0';
		$pktotal2='0';
		$pkuid='0';

		// PK处理start
		$key1='LivePK';
		$key2='LivePK_gift';
		
		$pkuid=\App\hGet($key1,$liveuid);
		if($pkuid){
			$ispk='1';
			\App\hIncrByFloat($key2,$liveuid,$total);
			
			$gift_uid=\App\hGet($key2,$liveuid);
			$gift_pkuid=\App\hGet($key2,$pkuid);
			
			$pktotal1=$gift_uid;
			$pktotal2=$gift_pkuid;
			
			$pkuid1=$liveuid;
			$pkuid2=$pkuid;
			
		}
		
	
		$aaa=\App\zIncrBy('user_'.$stream,$total,$uid);
	
		//获取主播映票
		$votestotal=$this->getVotes($liveuid);
		
		//获取用户余额及等级
		$userinfo2 =\PhalApi\DI()->notorm->user
				->select('consumption,coin')
				->where('id = ?', $uid)
				->fetchOne();
		$level=\App\getLevel($userinfo2['consumption']);


		$touserinfo=\App\getUserInfo($liveuid);
		$sendgift_info=array(
	            "uid"=>$uid,
	            "touid"=>$liveuid,
	            "to_username"=>$touserinfo['user_nickname'],
	            "giftid"=>$giftid,
	            "type"=>$giftinfo['type'],
	            "giftcount"=>$giftcount,
	            "totalcoin"=>$total,
	            "giftname"=>$giftinfo['name'],
	            "giftname_en"=>$giftinfo['name_en'],
	            "gifticon"=>\App\get_upload_path($giftinfo['thumb']),
	            "swftime"=>$giftinfo['swftime'],
	            "swftype"=>$giftinfo['swftype'],
	            "swf"=>$swf,
	            "level"=>$level,
	            "coin"=>$userinfo2['coin'],
	            "votestotal"=>$votestotal,


	            
	            "ispk"=>$ispk,
	            "pkuid"=>$pkuid,
	            "pkuid1"=>$pkuid1,
	            "pkuid2"=>$pkuid2,
	            "pktotal1"=>$pktotal1,
	            "pktotal2"=>$pktotal2,
	        );
			
		$result['level']=$level;
		$result['coin']=$userinfo2['coin'];
		$result['sendgift_info'][]=$sendgift_info;

   
		

        // 清除缓存
		\App\delCache("userinfo_".$uid); 
		\App\delCache("userinfo_".$liveuid); 	


		return $result;
	}		
	
	/* 发送弹幕 */
	public function sendBarrage($uid,$liveuid,$stream,$giftid,$giftcount,$content) {

		$configpri=\App\getConfigPri();			 
		$giftinfo=array(
			"giftname"=>\PhalApi\T('弹幕'),
			"gifticon"=>'',
			"needcoin"=>$configpri['barrage_fee'],
		);		
		$total= $giftinfo['needcoin']*$giftcount;
		if($total<0){
            return 1002;
        }
        $addtime=time();
		$stream2=explode('_',$stream);
		$showid=$stream2[1];
		if(!$showid){
			$showid=0;
		}
		
		

        if($total>0){
			/* 更新用户余额 消费 */
			$ifok=\App\upCoin($uid,$total);
			if(!$ifok){
				/* 余额不足 */
				return 1001;
			}
			$insert_coin=[
				"type"=>'0',
				"action"=>'11',
				"uid"=>$uid,
				"touid"=>$liveuid,
				"actionid"=>$giftid,
				"nums"=>$giftcount,
				"totalcoin"=>$total,
				"showid"=>$showid,
				"addtime"=>$addtime
			];
			\App\addCoinRecord($insert_coin);
        	
			
			
			// 更新用户 魅力值 累计魅力值
			\App\addVotes($liveuid,$total,$total);
			$insert_votes=[
				'type'=>'1',
				'action'=>'14',
				'uid'=>$liveuid,
				'fromid'=>$uid,
				'actionid'=>$giftid,
				'nums'=>$giftcount,
				'total'=>$total,
				'showid'=>$showid,
				'votes'=>$total,
				'addtime'=>time(),
			];
			\App\addVoteRecord($insert_votes);


        }

        

		//获取用户余额及等级
		$userinfo2 =\PhalApi\DI()->notorm->user
				->select('consumption,coin')
				->where('id = ?', $uid)
				->fetchOne();
		$level=\App\getLevel($userinfo2['consumption']);
		
		/* 清除缓存 */
		\App\delCache("userinfo_".$uid); 
		\App\delCache("userinfo_".$liveuid); 
		
		$votestotal=$this->getVotes($liveuid);
		
		$barragetoken=md5(md5($uid.$liveuid.$giftid.$giftcount.$total.$showid.$addtime.rand(100,999)));
		 
		$result=array(
			"uid"=>$uid,
			"content"=>$content,
			"giftid"=>$giftid,
			"giftcount"=>$giftcount,
			"totalcoin"=>$total,
			"giftname"=>$giftinfo['giftname'],
			"gifticon"=>$giftinfo['gifticon'],
			"level"=>$level,
			"coin"=>$userinfo2['coin'],
			"votestotal"=>$votestotal,
			"barragetoken"=>$barragetoken
		);
		
		return $result;
	}			
	
	/* 设置/取消 管理员 */
	public function setAdmin($liveuid,$touid){
					
		$isexist=\PhalApi\DI()->notorm->live_manager
					->select("*")
					->where('uid=? and  liveuid=?',$touid,$liveuid)
					->fetchOne();			
		if(!$isexist){
			$count =\PhalApi\DI()->notorm->live_manager
						->where('liveuid=?',$liveuid)
						->count();	
			if($count>=5){
				return 1004;
			}

			$rs=\PhalApi\DI()->notorm->live_manager
					->insert(
						array(
							"uid"=>$touid,
							"liveuid"=>$liveuid
						)
					);	

			if($rs!==false){
				return 1;
			}else{
				return 1003;
			}				
			
		}else{
			$rs=\PhalApi\DI()->notorm->live_manager
				->where('uid=? and  liveuid=?',$touid,$liveuid)
				->delete();		
			if($rs!==false){
				return 0;
			}else{
				return 1003;
			}						
		}
	}
	
	/* 管理员列表 */
	public function getAdminList($liveuid){
		$rs=\PhalApi\DI()->notorm->live_manager
						->select("uid")
						->where('liveuid=?',$liveuid)
						->fetchAll();	
		foreach($rs as $k=>$v){
			$rs[$k]=\App\getUserInfo($v['uid']);
		}	

        $info['list']=$rs;
        $info['nums']=(string)count($rs);
        $info['total']='5';
		return $info;
	}
    

	

	
	/* 主播总映票 */
	public function getVotes($liveuid){
		$userinfo=\PhalApi\DI()->notorm->user
					->select("votestotal")
					->where('id=?',$liveuid)
					->fetchOne();	
		return $userinfo['votestotal'];					
	}
    
    /* 是否禁言 */
	public function checkShut($uid,$liveuid){
        
        $isexist=\PhalApi\DI()->notorm->live_shut
                ->where('uid=? and liveuid=? ',$uid,$liveuid)
                ->fetchOne();
        if($isexist){
            \App\hSet($liveuid . 'shutup',$uid,1);
        }else{
            \App\hDel($liveuid . 'shutup',$uid);
            return 0;
        }
		return 1;			
	}

    /* 禁言 */
	public function setShutUp($uid,$liveuid,$touid,$showid){
        
        $isexist=\PhalApi\DI()->notorm->live_shut
                ->where('uid=? and liveuid=? ',$touid,$liveuid)
                ->fetchOne();
				
				
		$data=[
			'uid'=>$touid,
			'liveuid'=>$liveuid,
			'actionid'=>$uid,
			'showid'=>$showid,
			'addtime'=>time()
		];
        if($isexist){
            if($isexist['showid']==$showid){
                return 1002;
            }
            if($isexist['showid']==0 && $showid!=0){
                return 1002;
            }
            $rs=\PhalApi\DI()->notorm->live_shut
            	->where('id=?',$isexist['id'])
            	->update($data);
        }else{
            $rs=\PhalApi\DI()->notorm->live_shut
            	->insert($data);
        }
        
        
        
		return $rs;			
	}
    
    /* 踢人 */
	public function kicking($uid,$liveuid,$touid){
        
        $isexist=\PhalApi\DI()->notorm->live_kick
                ->where('uid=? and liveuid=? ',$touid,$liveuid)
                ->fetchOne();
        if($isexist){
            return 1002;
        }
        
        $rs=\PhalApi\DI()->notorm->live_kick
        	->insert(
        		[
        			'uid'=>$touid,
        			'liveuid'=>$liveuid,
        			'actionid'=>$uid,
        			'addtime'=>time()
        		]
        	);
        
        
		return $rs;
	}
      
	
	/* 超管关闭直播间 */
	public function superStopRoom($uid,$liveuid,$type,$banruleid){
		
		$userinfo=\PhalApi\DI()->notorm->user
					->select("issuper")
					->where('id=? ',$uid)
					->fetchOne();
		
		if($userinfo['issuper']==0){
			return 1001;
		}
		
		if($type==1){

			$nowtime=time();
			
            // 禁播列表
            $isexist=\PhalApi\DI()->notorm->live_ban
				->where('liveuid=? ',$liveuid)
				->fetchOne();
            if($isexist){
                return 1002;
            }

            $rules=\App\getLiveBanRules();
			
            $current_rules=[];
            foreach ($rules as $k => $v) {
            	if($banruleid==$v['id']){
            		$current_rules=$v;
            		break;
            	}
            }
			
			

            $ban_type=$current_rules['type'];
            switch ($ban_type) {
            	case '30min':
            		$endtime=$nowtime+30*60;
            		break;

            	case '1day':
        			$endtime=strtotime("+1 day");
        		break;

	        	case '7day':
	        		$endtime=strtotime("+7 day");
	        		break;

	        	case '15day':
	        		$endtime=strtotime("+15 day");
	        		break;

	        	case '30day':
	        		$endtime=strtotime("+30 day");
	        		break;

	        	case '90day':
	        		$endtime=strtotime("+90 day");
	        		break;

	        	case '180day':
	        		$endtime=strtotime("+180 day");
	        		break;

	        	case 'all':
	        		$endtime=0;
	        		break;
	        	
	        	default:
	        		$endtime=0;
	        		break;

            }
			


            \PhalApi\DI()->notorm->live_ban->insert(
            	[
            		'liveuid'=>$liveuid,
            		'superid'=>$uid,
            		'type'=>$ban_type,
            		'addtime'=>$nowtime,
            		'endtime'=>$endtime
            	]);
		}
        
        if($type==2){
            //关闭并禁用
			\PhalApi\DI()->notorm->user
				->where('id=? ',$liveuid)
				->update(array('user_status'=>0));
        }
		
	
		$info=\PhalApi\DI()->notorm->live
				->select("stream")
				->where('uid=? and islive="1"',$liveuid)
				->fetchOne();
		if($info){
           $this->stopRoom($liveuid,$info['stream']);
		}

		
		return 0;
		
	}
    
   

    /* 检测房间状态 */
    public function checkLiveing($uid,$stream){
        $info=\PhalApi\DI()->notorm->live
                ->select('uid')
				->where('uid=? and stream=? ',$uid,$stream)
				->fetchOne();
        if($info){
            return '1';
        }
        
        return '0';
    }
    
    /* 获取直播信息 */
    public function getLiveInfo($liveuid){
        
        $info=\PhalApi\DI()->notorm->live
					->select('uid,title,city,stream,pull,thumb,isvideo,type,type_val,anyway,starttime,isudata')
					->where('uid=?',$liveuid)
					->fetchOne();
        if($info){
            $info=\App\handleLive($info);
        }
        return $info;
    }
	
	
	 /* 获取用户本场贡献 */
    public function getContribut($uid,$liveuid,$showid){
        $sum=\PhalApi\DI()->notorm->user_coinrecord
				->where('action=1 and uid=? and touid=? and showid=? ',$uid,$liveuid,$showid)
				->sum('totalcoin');
        if(!$sum){
            $sum=0;
        }
        
        return (string)$sum;
    }
	
	/* 直播间内查看用户资料开关 */
    public function setUserData($uid,$stream){
		
		$isexist=\PhalApi\DI()->notorm->live
					->select("isudata")
					->where('uid=? and stream=? ',$uid,$stream)
					->fetchOne();
		$isudata=0;
		if($isexist['isudata']==0){
			$isudata=1;
		}
		
		\PhalApi\DI()->notorm->live
					->where('uid = ?', $uid)
					->update(['isudata'=>$isudata]);
       
		return $isudata;
      
    }




}
